package com.qf.interceptor;

import com.qf.constant.BootConstant;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author TZL
 * @date 2020/8/7
 * 拦截器
 */
public class UpInterceptor implements HandlerInterceptor {
    //判断文件大小
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ServletRequestContext context = new ServletRequestContext(request);
        //获取请求的 内容大小
        long length = context.contentLength();
        if (length> BootConstant.MAXSIZE){
            throw new MaxUploadSizeExceededException(BootConstant.MAXSIZE);
        }
        return true;


    }
}
